package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.constants.WorkerCertificationConstants;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

    @Resource
    private WorkerCertificationMapper workerCertificationMapper;
    @Resource
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;
    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 服务人员认证提交
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void workerCertificationAudit(WorkerCertificationAuditAddReqDTO dto) {
        // 参数校验
        if(ObjUtil.isNull(dto)){
            throw new IllegalArgumentException("服务人员认证提交参数异常");
        }
        // 判断是否已经提交过
        Long count = this.lambdaQuery()
                .eq(WorkerCertification::getIdCardNo, dto.getIdCardNo())
                .ne(WorkerCertification::getCertificationStatus, WorkerCertificationConstants.FAIL)
                .count();
        if(count>0){
            throw new ForbiddenOperationException("服务人员认证已提交");
        }
        // 判断当前用户有无被拒绝的认证
        //  获取当前用户id
        Long userId = UserContext.currentUserId();
        WorkerCertification workerCertification1 = this.lambdaQuery()
                .eq(WorkerCertification::getCertificationStatus, WorkerCertificationConstants.FAIL)
                .eq(WorkerCertification::getId, userId)
                .one();
        if(ObjectUtil.isNotNull(workerCertification1)){
            // 删除该认证记录
            this.removeById(workerCertification1.getId());
        }
        WorkerCertification workerCertification = BeanUtil.toBean(dto, WorkerCertification.class);
        // 获取当前服务人员的id
        workerCertification.setId(UserContext.currentUserId());
        workerCertification.setCertificationStatus(1);
        workerCertification.setCreateTime(LocalDateTime.now());
        workerCertification.setUpdateTime(LocalDateTime.now());
        WorkerCertificationAudit workerCertificationAudit = BeanUtil.copyProperties(workerCertification, WorkerCertificationAudit.class,"id");
        workerCertificationAudit.setServeProviderId(UserContext.currentUserId());
        save(workerCertification);
        workerCertificationAuditMapper.insert(workerCertificationAudit);
    }

    /**
     * 获取拒绝原因
     * @return
     */
    @Override
    public RejectReasonResDTO getRejectReason() {
        // 获取当前用户
        Long userId = UserContext.currentUserId();
        String rejectReason = workerCertificationMapper.getRejectReason(userId);
        return new RejectReasonResDTO(rejectReason);
    }
}
